#!/bin/sh

ROOT="$(git rev-parse --show-toplevel)"
HEIGHT=13
WIDTH=51

check_retval(){
  case $1 in
    1|255)
      echo "Stopped"
      exit;;
  esac
}

check_input(){
  if [ "$1" = "" ] 
  then  
    echo "Can't be empty" 
    exit 
  fi
}

exec 3>&1
domain_root=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "Domain root:" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-
check_input $domain_root


exec 3>&1
sub_domain=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "Subdomain (can be empty):" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-

exec 3>&1
signed=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --menu "Use certificate signed by Let's Encrypt?" $HEIGHT $WIDTH 2 \
    "Y" "" \
    "n" "" \
  2>&1 1>&3)
check_retval $?
exec 3>&-

[ "${signed}" = "${signed#[Yy]}" ]
signed=$?


if [ $signed -ne 0 ]; then
  exec 3>&1
  email=$(dialog \
    --title "Configure" \
    --clear \
    --ok-label "Ok" \
    --cancel-label "Exit" \
    --inputbox "Email:" $HEIGHT $WIDTH \
    2>&1 1>&3)
  check_retval $?
  exec 3>&-
  check_input $email
fi

if [ -z "$sub_domain" ]
then
  domain="${domain_root}"
else
  domain="${sub_domain}.${domain_root}"
fi

mkdir -p $ROOT/docker/bootstrap


cat > $ROOT/docker/bootstrap/bootstrap.env <<EOF
#!/bin/sh
email=${email}
domain=${domain}
domain_root=${domain_root}
signed=${signed}
EOF

chmod +x ./docker/bootstrap/bootstrap.env

docker-compose -f docker/bootstrap/bootstrap.yaml up

git_dir=$PWD
while [ ! -d .git ]; do
    git_dir=$(dirname "${git_dir}")
done

cd "${git_dir}" || exit

if [ ! -f ./docker/bootstrap/bootstrap.env ]; then
   printf "bootstrap.env missing! Please run the bootstrap_certificates script.\n"
   exit 1
fi

. ./docker/bootstrap/bootstrap.env

exec 3>&1
dbms=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --menu "Select DBMS:" $HEIGHT $WIDTH 2 \
    "postgres" "" \
    "mariadb" "" \
  2>&1 1>&3)
check_retval $?
exec 3>&-

exec 3>&1
db=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "GNU social database name:" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-

if [ "${dbms}" = 'mariadb' ] 
then
  exec 3>&1
  user=$(dialog \
    --title "Configure" \
    --clear \
    --ok-label "Ok" \
    --cancel-label "Exit" \
    --inputbox "Database user:" $HEIGHT $WIDTH \
    2>&1 1>&3)
  check_retval $?
  exec 3>&-
  check_input $user
fi


exec 3>&1
password=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "Database password:" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-
check_input $password

exec 3>&1
sitename=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "Sitename:" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-
check_input $sitename

exec 3>&1
admin_nick=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "Admin nickname:" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-
check_input $admin_nick

exec 3>&1
admin_password=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "Admin password:" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-
check_input $admin_password

exec 3>&1
profile=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --menu "Site profile:" $HEIGHT $WIDTH 4 \
    "public"  "" \
    "private"  "" \
    "community"  "" \
    "single_user" "" \
  2>&1 1>&3)
check_retval $?
exec 3>&-

exec 3>&1
mailer_dsn=$(dialog \
  --title "Configure" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
  --inputbox "Mailer dsn:" $HEIGHT $WIDTH \
  2>&1 1>&3)
check_retval $?
exec 3>&-
check_input $mailer_dsn

mkdir -p $ROOT/docker/db

if [ "${dbms}" = 'mariadb' ]; then
  exec 3>&1
  db_root_password=$(dialog \
    --title "Configure" \
    --clear \
    --ok-label "Ok" \
    --cancel-label "Exit" \
    --inputbox "DB root password" $HEIGHT $WIDTH \
    2>&1 1>&3)
  check_retval $?
  exec 3>&-
  check_input $db_root_password

  cat > $ROOT/docker/db/db.env <<EOF
DBMS=${dbms}
MYSQL_ROOT_PASSWORD=${db_root_password}
EOF

    database_url="DATABASE_URL=mysql://${user}:${password}@db:3306/${db}"

else
    cat > $ROOT/docker/db/db.env <<EOF
DBMS=${dbms}
POSTGRES_USER=postgres
POSTGRES_PASSWORD=${password}
EOF

    user='postgres'
    database_url="DATABASE_URL=postgresql://${user}:${password}@db:5432/${db}"

fi

echo "${database_url}" >> .env.local

mkdir -p $ROOT/docker/social

cat > $ROOT/docker/social/social.env <<EOF
SOCIAL_DBMS="${dbms}"
SOCIAL_DB="${db}"
SOCIAL_USER="${user}"
SOCIAL_PASSWORD="${password}"
SOCIAL_DOMAIN="${domain}"
SOCIAL_SITENAME="${sitename}"
SOCIAL_ADMIN_NICK="${admin_nick}"
SOCIAL_ADMIN_PASSWORD="${admin_password}"
SOCIAL_ADMIN_EMAIL="${email}"
SOCIAL_SITE_PROFILE="${profile}"
MAILER_DSN="${mailer_dsn}"
EOF


exec 3>&1
docker_compose=$(dialog \
  --title "Services" \
  --clear \
  --ok-label "Ok" \
  --cancel-label "Exit" \
    --checklist "Services to include in docker-compose:" $HEIGHT $WIDTH 6 \
    "nginx" "" on \
    "certbot" "" on \
    "php" "" on \
    "db" "" on \
    "redis" "" on \
    "mail" "" on \
  2>&1 1>&3)
check_retval $?
exec 3>&-

echo "version: '3.3'" > docker-compose.yaml
echo "\nservices:" >> docker-compose.yaml

case $docker_compose in *"nginx"*)
  $ROOT/docker/social/nginx
esac

case $docker_compose in *"certbot"*)
  $ROOT/docker/social/certbot
esac

case $docker_compose in *"php"*)
  $ROOT/docker/social/php
esac

case $docker_compose in *"db"*)
  $ROOT/docker/social/db
esac

case $docker_compose in *"redis"*)
  $ROOT/docker/social/redis
esac

case $docker_compose in *"mail"*)
  $ROOT/docker/social/mail
  exec 3>&1
  mail_domain_root=$(dialog \
    --title "Configure Mail" \
    --clear \
    --ok-label "Ok" \
    --cancel-label "Exit" \
    --inputbox "E-mail domain root:" $HEIGHT $WIDTH \
    2>&1 1>&3)
  check_retval $?
  exec 3>&-
  check_input $mail_domain_root

  exec 3>&1
  mail_subdomain=$(dialog \
    --title "Configure Mail" \
    --clear \
    --ok-label "Ok" \
    --cancel-label "Exit" \
    --inputbox "E-mail subdomain (can be empty):" $HEIGHT $WIDTH \
    2>&1 1>&3)
  check_retval $?
  exec 3>&-

  exec 3>&1
  mail_user=$(dialog \
    --title "Configure Mail" \
    --clear \
    --ok-label "Ok" \
    --cancel-label "Exit" \
    --inputbox "E-mail user (name without @domain): " $HEIGHT $WIDTH \
    2>&1 1>&3)
  check_retval $?
  exec 3>&-
  check_input $mail_user

  exec 3>&1
  mail_pass=$(dialog \
    --title "Configure Mail" \
    --clear \
    --ok-label "Ok" \
    --cancel-label "Exit" \
    --inputbox "E-mail user password: " $HEIGHT $WIDTH \
    2>&1 1>&3)
  check_retval $?
  exec 3>&-
  check_input $mail_pass
  
  mkdir -p $ROOT/docker/mail

  cat > $ROOT/docker/mail/mail.env <<EOF
  MAIL_DOMAIN_ROOT="${mail_domain_root}"
  MAIL_SUBDOMAIN="${mail_subdomain}"
  MAIL_USER="${mail_user}"
  MAIL_PASSWORD="${mail_pass}"
EOF
  
  $ROOT/docker/mail/setup.sh

esac

echo "volumes:\n  database:" >> docker-compose.yaml





